// author: Dmitriy Zabranskiy 2012(c)
//

import java.util.List;
import java.util.Random;

public class Exemplar implements Cloneable {
    // The exemplar in our case is a schedule, its rating and additional info
    private static int lecture_count;
    private int[] schedule = new int[lecture_count]; // Schedule, index of the tutor is stored in the cells
    private static Random random = new Random();
    private static Tutor[] tutors;
    private double rating; // Rating is calculated by the formula devised by us (Assessment)

    public Exemplar() {
        for (int j = 0; j < lecture_count; j++) {
            schedule[j] = random.nextInt(tutors.length);
        }
        calcRating();
    }

    public Exemplar(int[] schedule) {
        this.schedule = schedule;
        calcRating();
    }

    public void mutate() {
        schedule[random.nextInt(lecture_count - 1)] = random
                .nextInt(tutors.length);
    }

    public static void setTutors(List<Tutor> tutors) {
        int tutorLen = tutors.size();
        Exemplar.tutors = new Tutor[tutorLen];
        for (int i = 0; i < tutorLen; i++) {
            Exemplar.tutors[i] = tutors.get(i);
        }
    }
    // Solve the rating of an exemplar
    public void calcRating() {
        double result = 0;
        for (int i = 0; i < lecture_count; i++) {
            double priority = tutors[schedule[i]].getPriority();
            if (tutors[schedule[i]].getWishes()[i]) {
                result += priority;
            } else {
                result -= priority;
            }
        }
        this.rating = (result >= 0) ? result : 0;
    }

    public static void setLectureCount(int lecture_count) {
        Exemplar.lecture_count = lecture_count;
    }

    public static Tutor[] getTutors() {
        return tutors;
    }

    public int[] getSchedule() {
        return schedule;
    }

    public double getRating() {
        return rating;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Exemplar obj = (Exemplar) super.clone();
        int[] schedule = new int[lecture_count];
        System.arraycopy(this.schedule, 0, schedule, 0, lecture_count);
        obj.schedule = schedule;
        obj.rating = this.getRating();

        return obj;
    }

    @Override
    public String toString() {
        calcRating();
        StringBuilder str = new StringBuilder();
        str.append("rating ").append(this.rating).append(" \n");
        for (int aSchedule : schedule) {
            str.append(aSchedule);
            str.append(" ");
        }
        return str.toString();
    }
}
